package com.muchfish.reactor.api;

import reactor.core.publisher.Flux;

public class ReactorBackpressureExample {
    public static void main(String[] args) {
        Flux<Integer> source = Flux.range(1, 1000);

        // 设置缓冲区大小
        Flux<Integer> buffered = source.onBackpressureBuffer(10);

        // 使用背压策略
        buffered.subscribe(
                data -> {
                    // 模拟慢速消费者
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(data);
                },
                error -> System.err.println("Error: " + error),
                () -> System.out.println("Done"),
                subscription -> subscription.request(Long.MAX_VALUE) // 请求所有数据
        );
    }
}


